﻿using System;
using System.Collections.Concurrent;
using System.Linq;
using System.Web;
using Ue.CQRS.Samples.Domain;

namespace Ue.CQRS.Samples.Application.Command.Repositories
{
    public class DomainRepository : IDomainRepository
    {
        private static ConcurrentDictionary<Guid, IEntity> caches=new ConcurrentDictionary<Guid,IEntity>();

        public T Load<T>(Guid id) 
            where T : IEntity
        {
            IEntity entity;
            if (caches.TryGetValue(id, out entity))
            {
                return (T)entity;
            }

            return default(T);
        }


        public void Save(IEntity entity)
        {
            caches.AddOrUpdate(entity.ID, entity, (id, old) => entity);
        }
    }
}